//
//  MineBuySubViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineBuySubViewController.h"
#import "MineBuyCell.h"
@interface MineBuySubViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray; 
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation MineBuySubViewController
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageIndex = 1;
}

- (void)setupUI
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[MineBuyCell class] forCellReuseIdentifier:@"MineBuyCell"];
    [self.view addSubview:_tableView];
    CGFloat heights = Height_NavBar + 52;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.offset(-heights);
//        make.top.equalTo(headerImg.mas_bottom).offset(10);
//        make.height.mas_equalTo(220);
    }];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex = 1;
        [self requestDataWithParamDic:@{@"pay_status":@(self.buyType)}];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex ++;
        [self requestDataWithParamDic:@{@"pay_status":@(self.buyType)}];
    }];
}

- (void)requestDataWithParamDic:(NSDictionary *)paramDic
{
    
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"orderlist" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (self.pageIndex == 1) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:responseObject[@"data"][@"order_list"][@"list"]];
        } else {
            [self.dataArray addObjectsFromArray:responseObject[@"data"][@"order_list"][@"list"]];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.pageIndex == [responseObject[@"data"][@"order_list"][@"page_sum"] integerValue]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(void)setBuyType:(NSInteger)buyType
{
    _buyType = buyType;
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

/**
 可选实现，列表将要显示的时候调用
 */
- (void)listWillAppear
{
    if (self.buyType == 0) { // 已购买
        NSDictionary *paramDic = @{
            @"pay_status":@"1"
        };
//        [self requestDataWithParamDic:paramDic];
    } else if (self.buyType == 1) { // d待付款
        NSDictionary *paramDic = @{
            @"pay_status":@"0"
        };
//        [self requestDataWithParamDic:paramDic];
    }

}


- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
#define COLOR_WITH_RGB(R,G,B,A) [UIColor colorWithRed:R green:G blue:B alpha:A]
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineBuyCell"];
    if (self.buyType == 0) {
        cell.showBuyBtn = NO;
    } else if (self.buyType == 1) {
        cell.showBuyBtn = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 126;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
